<template>
<!--hotmall/packageB/addaddress/addaddress.wxml-->
<form action report-submit="true" @submit="submit">
  <view class="items">
    <text>姓名：</text>
    <input name="name" placeholder="请输入姓名" @input="formName" :value="name"></input>
  </view>
  <view class="items">
    <text>手机号：</text>
    <input name="phone" placeholder="请输入手机号" @input="formPhone" :value="phone" type="number" maxlength="11"></input>
  </view>
 <!-- <view class="items">
  <picker mode="multiSelector" bindchange="bindMultiPickerChange" bindcolumnchange="bindMultiPickerColumnChange" value="{{multiIndex}}" range="{{multiArray}}">
    <view class="picker">
      选择地区：{{multiArray[0][multiIndex[0]]}}，{{multiArray[1][multiIndex[1]]}}，{{multiArray[2][multiIndex[2]]}}
    </view>
  </picker>
</view> -->
  <view class="items">
    <text>省份：</text>
    <picker @change="bindPickerChange" :value="index" :range="array">
      <view class="picker">
        {{array[index]}}
      </view>
    </picker>
  </view>
  <view class="items">
    <text>城市：</text>
    <picker @change="bindPickerChangeOne" :value="indexone" :range="arrayone">
      <view class="picker">
        <input :value="arrayone[indexone]" disabled="true"></input>
      </view>
    </picker>
  </view>
  <view class="items">
    <text>县区：</text>
    <picker @change="bindPickerChangeTwo" :value="indextwo" :range="arraytwo">
      <view class="picker">
        <input :value="arraytwo[indextwo]" @input="getvalue" disabled="true">{{arraytwo[indextwo]}}</input>
      </view>
    </picker>
  </view>
  <view class="items">
    <text>详细地址：</text>
    <input name="dizhi" placeholder="请输入详细地址，如楼道、楼盘号等" @input="formDizhi" :value="dizhi"></input>
  </view>
  <radio-group @change="checkFun">
    <checkbox color="#8DE152" @change="checkFun" value="1"></checkbox>设置为默认地址
  </radio-group>
  <button @tap="submitId" formType="submit">保存</button>
</form>
</template>

<script>
import api from "../../../api";
var app = getApp();

export default {
  data() {
    return {
      array: [],
      arrayone: [],
      arraytwo: [],
      arrayid: [],
      arrayoneid: [],
      arraytwoid: [],
      objectarray: [],
      objectarrayone: [],
      objectarraytwo: [],
      province: [],
      index: 0,
      indexone: 0,
      indextwo: 0,
      customItem: '',
      region: [],
      provinceid: 1,
      //省id
      provinceidone: 1,
      //省id
      provinceidtwo: 1,
      //省id
      name: '',
      //姓名
      phone: '',
      dizhi: '',
      check: '',
      county: '',
      uid: 0,
      isop: 0,
      multiArray: [[], [], []],
      abc: 0,
      provinceName: "",
      cityName: "",
      countyName: "",
      multiIndex: "",
      le: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    if (options.abc) {
      this.setData({
        abc: 1
      });
    }

    if (options.res) {
      var data = JSON.parse(options.res); // console.log(data)

      this.setData({
        name: data.userName,
        phone: data.telNumber,
        provinceName: data.provinceName,
        cityName: data.cityName,
        countyName: data.countyName,
        dizhi: data.detailInfo,
        isop: 1
      });
    }

    this.getaddress();
    wx.getStorage({
      key: 'userId',
      success: res => {
        // console.log(res)
        this.setData({
          uid: res.data
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    bindMultiPickerChange: function (e) {
      console.info('picker发送选择改变，携带值为', e.detail.value);
      this.setData({
        multiIndex: e.detail.value
      });
    },

    //比较省
    countProvince() {
      var array = this.array,
          arrayid = this.arrayid;
      var province = this.provinceName;
      province = province.substr(0, 2); // console.log(province)
      // console.log(array)

      var l = 0;

      for (var i = 0; i < array.length; i++) {
        if (array[i] == province) {
          // console.log('索引值：' + i)
          // console.log(province)
          // console.log(arrayid[i])
          this.setData({
            provinceid: arrayid[i],
            index: i
          });
        }
      }
    },

    //比较市
    countCity() {
      var array = this.arrayone,
          arrayid = this.arrayoneid;
      var city = this.cityName;
      city = city.substr(0, 2); // console.log(city)
      // console.log(array)

      var l = 0;

      for (var i = 0; i < array.length; i++) {
        if (array[i] == city) {
          // console.log('索引值：' + i)
          // console.log(city)
          // console.log(arrayoneid[i])
          this.setData({
            provinceidone: arrayid[i],
            indexone: i
          });
        }
      }
    },

    //比较县
    countCounty() {
      var array = this.arraytwo,
          arrayid = this.arraytwoid;
      var county = this.countyName; // county = county.substr(0, 2)
      // console.log(county)
      // console.log(array)

      var l = 0;

      for (var i = 0; i < array.length; i++) {
        if (array[i] == county) {
          // console.log('索引值：' + i)
          // console.log(city)
          // console.log(arrayoneid[i])
          this.setData({
            provinceidtwo: arrayid[i],
            indextwo: i
          });
        }
      }
    },

    //获取省
    getaddress() {
      app.globalData.request({
        url: api.default.get_region,
        data: {
          parent_id: this.provinceid
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log('省', res);
          let name = [],
              index = [];

          for (let i = 0; i < res.length; i++) {
            name = name.concat(res[i].region_name);
            index = index.concat(res[i].region_id); // console.log(name)
          }

          this.setData({
            objectarray: res,
            array: name,
            arrayid: index
          }); // console.log(this.data.objectarray)
          // console.log(this.data.array)
          // console.log(this.data.arrayid)

          if (this.isop == 1) {
            this.countProvince();
            this.getaddressone();
          }
        }
      });
    },

    //获取市
    getaddressone() {
      console.info(this.provinceid);
      app.globalData.request({
        url: api.default.get_region,
        data: {
          parent_id: this.provinceid
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log('市', res);
          let name = [],
              index = [];

          for (let i = 0; i < res.length; i++) {
            name = name.concat(res[i].region_name);
            index = index.concat(res[i].region_id); // console.log(name)
          }

          this.setData({
            objectarrayone: res,
            arrayone: name,
            arrayoneid: index
          });
          console.info(name); // console.log(this.data.objectarray)
          // console.log(this.data.array)
          // console.log(this.data.arrayid)

          if (this.isop == 1) {
            this.countCity();
            this.getaddresstwo();
          }
        }
      });
    },

    //获取区
    getaddresstwo() {
      app.globalData.request({
        url: api.default.get_region,
        data: {
          parent_id: this.provinceidone
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值

        },
        success: res => {
          console.log('区', res);
          let name = [],
              index = [];

          for (let i = 0; i < res.length; i++) {
            name = name.concat(res[i].region_name);
            index = index.concat(res[i].region_id); // console.log(name)
          }

          this.setData({
            le: res.length,
            objectarraytwo: res,
            arraytwo: name,
            arraytwoid: index,
            county: name[0]
          }); // console.log(this.data.objectarray)
          // console.log(this.data.array)
          // console.log(this.data.arrayid)

          if (this.isop == 1) {
            this.countCounty(); // this.submit();
          }

          var allarr = [];
          allarr.push(this.array, this.arrayone, this.arraytwo);
          this.setData({
            multiArray: allarr
          });
          console.info(res);
        }
      });
    },

    //省
    bindPickerChange: function (e) {
      // console.log('picker发送选择改变，携带值为', e.detail.value)
      var id = this.arrayid[e.detail.value]; // console.log(id)

      this.setData({
        index: e.detail.value,
        provinceid: id
      });
      this.getaddressone();
    },
    //市
    bindPickerChangeOne: function (e) {
      // console.log('picker发送选择改变，携带值为', e.detail.value)
      var id = this.arrayoneid[e.detail.value]; // console.log(id)

      this.setData({
        indexone: e.detail.value,
        provinceidone: id
      });
      this.getaddresstwo();
    },
    //区
    bindPickerChangeTwo: function (e) {
      // console.log('picker发送选择改变，携带值为', e.detail.value)
      var id = this.arraytwoid[e.detail.value]; // console.log(id)

      this.setData({
        indextwo: e.detail.value,
        provinceidtwo: id
      });
    },

    //获取姓名
    formName(e) {
      this.setData({
        name: e.detail.value
      }); // console.log(this.data.name)
    },

    //获取电话
    formPhone(e) {
      this.setData({
        phone: e.detail.value
      }); // console.log(this.data.phone)
    },

    //获取详细地址
    formDizhi(e) {
      this.setData({
        dizhi: e.detail.value
      }); // console.log(this.data.dizhi)
    },

    checkFun: function (e) {
      // console.log(e)
      if (e.detail.value == '') {
        this.setData({
          check: 0
        });
      } else {
        this.setData({
          check: 1
        });
      }
    },

    submitId() {
      var aa = /^[1]([3-9])[0-9]{9}$/;

      if (this.uid == 0) {
        wx.showToast({
          title: '用户id不能为空'
        });
      } else if (this.name == undefined || this.name == '') {
        wx.showToast({
          title: '请输入姓名'
        });
      } else if (this.phone == undefined || this.phone == '') {
        wx.showToast({
          title: '请输入手机号'
        });
      } else if (!aa.test(this.phone)) {
        wx.showToast({
          title: '请输入正确的手机号',
          icon: 'none'
        });
      } else if (this.provinceid < 2) {
        wx.showToast({
          title: '请选择省份'
        });
      } else if (this.provinceidone == 1) {
        wx.showToast({
          title: '请选择城市'
        });
      } else if (this.provinceidtwo == 1) {
        this.setData({
          provinceidtwo: this.county
        });
        this.submit();
      } else if (this.le == 0) {
        wx.showToast({
          title: '11'
        });
        this.setData({
          le: 1,
          provinceidtwo: this.provinceidone
        });
        this.submit();
      } else if (this.dizhi == '' || this.dizhi == undefined || this.dizhi == null) {
        wx.showToast({
          title: '请输入详细地址'
        });
      } else {
        console.log(this.provinceidone);
        console.log(this.provinceidtwo);
        app.globalData.request({
          url: api.default.addaddress,
          data: {
            uid: this.uid,
            name: this.name,
            phone: this.phone,
            province: this.provinceid,
            city: this.provinceidone,
            area: this.provinceidtwo,
            addr: this.dizhi,
            is_default: this.check
          },
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            // console.log(res)
            wx.showToast({
              title: res.msg
            });

            if (this.abc == 1) {
              wx.redirectTo({
                url: '../../packageA/changeAddress/changeAddress'
              });
            } else {
              wx.navigateBack({
                delta: 1
              });
            }
          }
        });
      }
    },

    submit(e) {
      console.log(e);
      var a = e.detail.formId;
      wx.getStorage({
        key: 'userId',
        success: function (aaa) {
          console.log(aaa.data);
          var open = aaa.data;
          console.log(open);

          if (a == 'the formId is a mock one') {// wx.showToast({
            //   title: '请用手机测试',
            // })
          } else {
            console.log(a);
            app.globalData.request({
              url: api.gzSea.test,
              //仅为示例，并非真实的接口地址
              data: {
                form_id: a,
                user_id: open
              },
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值

              },

              success(res) {
                console.log(res);
              }

            });
          }
        }
      });
    }

  }
};
</script>
<style>
@import "./addaddress.css";
</style>